package com.hsj.demo;

import ws.schild.jave.process.ProcessWrapper;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hsj
 * @description:FfmpegExeLoader查看ffmpeg绝对路径
 * @date 2023-6-27 10:10
 */
public class FfmpegExeLoader {
    public static void main(String[] args) throws IOException {
        DefaultFFMPEGLocator defaultFFMPEGLocator = new DefaultFFMPEGLocator();
        System.out.println(defaultFFMPEGLocator.getExecutablePath());
        ProcessWrapper processWrapper = defaultFFMPEGLocator.createExecutor();
        processWrapper.addArgument("-version");
//        processWrapper.addArgument("F:\\__ffmpeg_all__\\ffmpeg-2023-06-26-git-285c7f6f6b-full_build\\test\\honer.mp4");
//        processWrapper.addArgument("http://spacefly.cn/assets2/corner.mp4");
        processWrapper.execute(false,true);
        InputStream errorStream = processWrapper.getErrorStream();
        OutputStream outputStream = processWrapper.getOutputStream();


        //打印执行结果
        StringBuilder info = new StringBuilder();
        int len = 0;
        while ((len=errorStream.read())!=-1){
            char t = (char)len;
            System.out.print(t);
            info.append(t);
        }

        byte[] bytes = new byte[2048];
        outputStream.write(bytes);
        System.out.println(new String(bytes, StandardCharsets.UTF_8));

        //正则解析时间
        Matcher matcher = patternDuration().matcher(info);
        //正则提取字符串
        while(matcher.find()){
            //such as:00:01:03.32
            System.out.println("************************************************************************************");
            System.out.println("duration:");
            System.out.println(matcher.group(1));
        }
    }

    //模式
    public static Pattern patternDuration() {
        //"(?i)duration:\\s*([0-9\\:\\.]+)"-->匹配到时分秒即可，毫秒不需要
        return Pattern.compile("(?i)duration:\\s*([0-9\\:\\.]+)");
    }

}
